import java.util.Scanner;


public class HomeWork {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Phone[] phones = new Phone[4];
        String s;
        int in;
        for (int i = 0; i < phones.length; i++) {
            Phone phone = new Phone();
            System.out.print("请输入第"+(i+1)+"部手机品牌：");
            s = sc.next();
            phone.setBrand(s);
            System.out.print("请输入第"+(i+1)+"部手机颜色：");
            s = sc.next();
            phone.setColour(s);
            System.out.print("请输入第"+(i+1)+"部手机价格：");
            in = sc.nextInt();
            phone.setPrice(in);
            phones[i] = phone;
        }

        for (int i = phones.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++){
                if (phones[j].getPrice() > phones[j+1].getPrice()){
                    Phone temp = phones[j];
                    phones[j] = phones[j+1];
                    phones[j+1] = temp;
                }
            }
        }

        System.out.println("************按照价格从低到高************");
        for (int i = 0; i < phones.length; i++) {
            System.out.println("品牌:"+phones[i].getBrand()+"\t"+
                    "颜色:"+phones[i].getColour()+"\t"+
                    "价格:"+phones[i].getPrice());
        }

        System.out.println("\n************最高价格手机************");
        Phone phone = maxPhone(phones);;
        System.out.println("品牌:"+phone.getBrand()+"\t"+
                "颜色:"+phone.getColour()+"\t"+
                "价格:"+phone.getPrice());

        System.out.println("\n************最低价格手机************");
        Phone phone1 = minPhone(phones);;
        System.out.println("品牌:"+phone1.getBrand()+"\t"+
                "颜色:"+phone1.getColour()+"\t"+
                "价格:"+phone1.getPrice());

        System.out.println("\n************查询手机************");
        System.out.println("输入手机品牌首字母:");
        s=sc.next();
        Phone phone2 = inquirePhone(phones,s);;
        System.out.println("品牌:"+phone2.getBrand()+"\t"+
                "颜色:"+phone2.getColour()+"\t"+
                "价格:"+phone2.getPrice());




        System.out.println("\n************查找品牌首字母手机************");
        System.out.println("输入手机品牌首字母:");
        s=sc.next();
        printAllPhoneOfInitials(phones,s.charAt(0));


    }

    public static Phone maxPhone(Phone[] phones) {
        Phone phone = new Phone();
        phone = phones[0];
        for (Phone phone1 : phones) {
            if (phone.getPrice() < phone1.getPrice()) {
                phone = phone1;
            }
        }
        return phone;
    }

    public static Phone minPhone(Phone[] phones) {
        Phone phone = new Phone();
        phone = phones[0];
        for (Phone phone1 : phones) {
            if (phone.getPrice() > phone1.getPrice()){
                phone = phone1;
            }
        }
        return phone;
    }

    /**
     * 输入手机品牌首字母  查询出满足条件的手机 并打印到控制台
     */
    public static Phone inquirePhone(Phone[] phones,String s) {
        Phone phone = new Phone();
        for (Phone phone1 : phones) {
            if (phone1.getBrand().equals(s)){
                phone = phone1;
            }
        }
        return phone;
    }

    public  static  void printAllPhoneOfInitials(Phone[] phones,char ch){
        for (int i = 0; i < phones.length; i++) {
            if (phones[i].getBrand().indexOf(ch) == 0){
                System.out.println("品牌:"+phones[i].getBrand()+"\t"+
                        "颜色:"+phones[i].getColour()+"\t"+
                        "价格:"+phones[i].getPrice());
            }
        }
    }
}
